package at.fhv.components.sobel;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import at.fhv.framework.customizerTools.MatrixTablePanel;

/**
 * The DilateCustomizer class.
 * 
 * @author Michael Sieber
 */
public class SobelCustomizer extends JPanel implements Customizer, ActionListener 
{
	private static final long serialVersionUID = -5576304645898860859L;
	private PropertyChangeSupport _pcsupport = new PropertyChangeSupport(this);
	private JComboBox _sobelComboBox;
	private JButton _applyButton;
	private SobelFilter _sobelFilter;
	private Sobel _selectedSobel;
	private MatrixTablePanel _matrixTablePanel;

	public SobelCustomizer()
	{
		initGUI();
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void setObject(Object obj)
	{
		_sobelFilter = (SobelFilter) obj;
		_selectedSobel = _sobelFilter.getSobel();
		_sobelComboBox.setSelectedItem(_selectedSobel);
		
		setSobelAtGui();
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.addPropertyChangeListener(pcl);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl) 
	{
		_pcsupport.removePropertyChangeListener(pcl);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent evt) 
	{
		Object evtSource = evt.getSource();
		if (evtSource.equals(_applyButton))
		{
			_selectedSobel = (Sobel) _sobelComboBox.getSelectedItem();
			_sobelFilter.setSobel(_selectedSobel);
		}
		else if (evtSource.equals(_sobelComboBox))
		{
			_selectedSobel = (Sobel) _sobelComboBox.getSelectedItem();
			setSobelAtGui();
		}
	}

	/**
	 * Initialize the gui
	 */
	private void initGUI() 
	{
		setLayout(new BorderLayout());
			
		JPanel westPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		westPanel.add(new JLabel("Sobel"));
			
		JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		_sobelComboBox = new JComboBox(new DefaultComboBoxModel(Sobel.values()));
		_sobelComboBox.addActionListener(this);
		northPanel.add(_sobelComboBox);
		
		JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		_matrixTablePanel = new MatrixTablePanel();
		centerPanel.add(_matrixTablePanel);
		
		JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		_applyButton = new JButton("Apply");
		_applyButton.addActionListener(this);
		southPanel.add(_applyButton);
		
		add(westPanel, BorderLayout.WEST);
		add(northPanel, BorderLayout.NORTH);
		add(centerPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.SOUTH);
	}
	
	private void setSobelAtGui()
	{
		_matrixTablePanel.setTableData(_selectedSobel.getMatrix(), _selectedSobel.getColumns(), _selectedSobel.getRows());
	}
}
